package com.lingnan.club.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.club.mapper.SignMapper;
import com.lingnan.club.mapper.UserMapper;
import com.lingnan.club.pojo.Sign;
import com.lingnan.club.pojo.User;
import com.lingnan.club.service.SignService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.annotation.Target;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘宇浩
 * @since 2022-04-03
 *
 * * @author 邓仁鸿
 *  * @since 2022-04-05
 */
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements SignService {

    @Autowired
    private SignMapper signMapper;

    @Autowired
    private UserMapper userMapper;



    public boolean publishSign(Sign sign, List<Integer> userIds) {
        //将签到活动插入数据库
        int insert = signMapper.insert(sign);
        if (insert > 0){
            //选择需要签到的用户 => userIds
            for (int i = 0;i < userIds.size();i++){
                //获取出userId中的用户id
                Integer userId = userIds.get(i);
                //查询对应的所需签到的用户
                QueryWrapper<User> wrapper = new QueryWrapper<>();
                wrapper.eq("id",userId);
                User user = userMapper.selectOne(wrapper);
                //将需要签到的活动设置到数据库
                user.setSignId(sign.getId());
                user.setSignName(sign.getName());
                userMapper.update(user,wrapper);
                return true;
            }
        }
        return false;
    }


    @Override
    public Integer deleteSignById(Integer id) {
        return signMapper.deleteById(id);
    }

    @Override
    public int updateSignActive(Sign sign) { return signMapper.updateSignActive(sign); }

    @Override
    public List<Sign> findAll() {
        return signMapper.findAll();
    }

    @Override
    public boolean publishSignActive(Sign sign) { return signMapper.publishSignActive(sign); }

    @Override
    public List<Sign> searchSign(String name) {
        QueryWrapper<Sign> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
//                .between("begin_time","20","23")
//                .eq("end_time",30);
        System.out.println(name);
        List<Sign> list = signMapper.selectList(queryWrapper);
        return list;
    }


}
